package com.jied.duodian.web;

import com.jied.duodian.entity.DdAddress;
import com.jied.duodian.service.DdAddressService;
import com.jied.duodian.util.CommonResult;
import com.jied.duodian.util.SuId;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Description:
 *
 * @author LiJie
 * @date Created on 2020/11/5
 * WWW https://github.com/j6l
 */
@RestController
@Slf4j
@RequestMapping("/address")
public class addressController {


    @Autowired
    DdAddressService ddAddressService;
    @Autowired
    SuId suId;

    /**
     * 获取当前用户地址
     *
     * @param req
     * @return
     */
    @GetMapping("/get")
    public CommonResult getbyuser(HttpServletRequest req) {
        String s = suId.getuserName( req );
        List<DdAddress> list = ddAddressService.getAddressByusername( s );
        return CommonResult.success( list );
    }

    /**
     * 获取运营地址
     *
     * @return
     */
    @GetMapping("/adminget")
    public CommonResult getByadmin() {
        List<DdAddress> list = ddAddressService.getAddressByusername( "admin" );
        return CommonResult.success( list );
    }

    /**
     * 删除地址
     *
     * @return
     */
    @GetMapping("/del/{id}")
    public CommonResult delByid(@PathVariable("id") Integer id) {
        Integer integer = ddAddressService.delAddress( id );
        return CommonResult.success( integer );
    }

    @GetMapping("/getaddress/{id}")
    public CommonResult getaddress(@PathVariable("id") Integer id) {
        log.info( "查询了id为{}的地址", id );
        DdAddress ddAddress = ddAddressService.getAddressByid( id );
        return CommonResult.success( ddAddress );
    }

    /**
     * 修改地址
     *
     * @return
     */
    @PostMapping("/updateaddress")
    public CommonResult updateaddress(
            @RequestBody DdAddress ddAddress) {
        Integer list = ddAddressService.updataAddress( ddAddress );
        return CommonResult.success( list );
    }

    /**
     * 添加地址
     *
     * @return
     */
    @PostMapping("/addaddress")
    public CommonResult addaddress(
            @RequestBody DdAddress ddAddress,
            HttpServletRequest req) {
        ddAddress.setUserName( suId.getuserName( req ) );
        Integer list = ddAddressService.addAddressList( ddAddress );
        return CommonResult.success( list );
    }

}
